/*
 * Copyright (C) 2017 zhouyou(478319399@qq.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.zhouyou.http.demo.slice;

import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.ProgressDialogCallBack;
import com.zhouyou.http.demo.ResourceTable;
import com.zhouyou.http.demo.constant.ComParamContact;
import com.zhouyou.http.demo.view.ProgressDialog;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.subsciber.IProgressDialog;
import com.zhouyou.http.view.CancelableDialog;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.TextField;
import ohos.agp.window.dialog.ToastDialog;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.multimodalinput.event.KeyEvent;

public class LoginAbilitySlice extends AbilitySlice {
    private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "MY_TAG");
    private TextField mTextFieldEmail;
    private TextField mTextFieldPassword;
    private Button btnEmailSignIn;
    private  String result="";

    public String getResult() {
        return result;
    }

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_login);
        mTextFieldEmail = (TextField) findComponentById(ResourceTable.Id_text_field_email);
        mTextFieldPassword = (TextField) findComponentById(ResourceTable.Id_text_field_password);
        btnEmailSignIn = (Button) findComponentById(ResourceTable.Id_btn_email_sign_in);
        mTextFieldPassword.setKeyEventListener(new Component.KeyEventListener() {
            @Override
            public boolean onKeyEvent(Component component, KeyEvent keyEvent) {


                return false;
            }
        });


        mTextFieldEmail.setText("18688994275");
        mTextFieldPassword.setText("123456");
        btnEmailSignIn.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                attemptLogin();


            }
        });


    }

    private void attemptLogin() {

        String email = mTextFieldEmail.getText();
        String password = mTextFieldPassword.getText();


        onLogin(email, password);
    }

    /**
     * 用登录举例
     */
    public void onLogin(final String name, final String pass) {
        ToastDialog toastDialog = new ToastDialog(this);
        IProgressDialog mProgressDialog = new IProgressDialog() {
            @Override
            public CancelableDialog getDialog() {


                ProgressDialog dialog = new ProgressDialog(LoginAbilitySlice.this);
                dialog.setMessage("登录中...");
                return dialog;
            }
        };


        EasyHttp.post(ComParamContact.Login.PATH)
                .params(ComParamContact.Login.ACCOUNT, name)
                .params("userName", "AndServer")
                .sign(true)
                .timeStamp(true).execute(new ProgressDialogCallBack<String>(mProgressDialog, true, true) {
            @Override
            public void onSuccess(String s) {
                result=s;
                toastDialog.setText("登录成功！result:"+s).show();
            }
            @Override
            public void onError(ApiException e) {
                super.onError(e);
                HiLog.info(LABEL, "onError-----" + e.getMessage());
            }
        });
    }
    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
